package com.ruoyi.system.controller.basic;

import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.dto.VehicleDto;
import com.ruoyi.system.domain.vo.PageInfo;
import com.ruoyi.system.domain.vo.UserVo;
import com.ruoyi.system.domain.vo.VehicleVo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.utils.MyPageUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.db.basic.BasicVehicles;
import com.ruoyi.system.service.IBasicVehiclesService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 汽车管理Controller
 * 
 * @author ruoyi
 * @date 2023-07-25
 */
@RestController
@RequestMapping("/basic/vehicles")
public class BasicVehiclesController extends BaseController
{
    @Autowired
    private IBasicVehiclesService basicVehiclesService;

    @Resource
    private ISysUserService iSysUserService;

    /**
     * 查询汽车管理列表
     */
    @PreAuthorize("@ss.hasPermi('basic:vehicles:list')")
    @GetMapping("/list")
    public PageInfo<VehicleVo> list(BasicVehicles basicVehicles)
    {
        // startPage();
        List<VehicleDto> vehicleDtos = basicVehiclesService.selectBasicVehiclesList(basicVehicles);
        return  MyPageUtils.startPage(vehicleDtos.stream().map(e -> {
            return basicVehiclesService.DtoConvertToVo(e);
        }).collect(Collectors.toList()));
    }

    /**
     * 导出汽车管理列表
     */
    @PreAuthorize("@ss.hasPermi('basic:vehicles:export')")
    @Log(title = "汽车管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasicVehicles basicVehicles)
    {
        List<VehicleDto> vehicleDtos = basicVehiclesService.selectBasicVehiclesList(basicVehicles);
        ExcelUtil<VehicleDto> util = new ExcelUtil<VehicleDto>(VehicleDto.class);
        util.exportExcel(response, vehicleDtos, "汽车管理数据");
    }

    /**
     * 获取汽车管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:vehicles:query')")
    @GetMapping(value = "")
    public AjaxResult getInfo(@RequestParam(value = "vehicleId",required = false) Long vehicleId, @RequestParam(value = "postId",required = true) Long postId)
    {
        List<UserVo> availableDrivers = iSysUserService.getAvailableDrivers(vehicleId, postId);
        VehicleVo vehicleVo = new VehicleVo();
        if(vehicleId != null)
        {
            VehicleDto vehicleDto = basicVehiclesService.selectBasicVehiclesByVehicleId(vehicleId);
            vehicleVo = basicVehiclesService.DtoConvertToVo(vehicleDto);
        }
        vehicleVo.setDrivers(availableDrivers.stream().map(e ->{
            return new UserVo(e.getUserId(),e.getUserName(),e.getNickName());
        }).collect(Collectors.toList()));
        return success(vehicleVo);
    }

    /**
     * 新增汽车管理
     */
    @PreAuthorize("@ss.hasPermi('basic:vehicles:add')")
    @Log(title = "汽车管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasicVehicles basicVehicles)
    {
        return toAjax(basicVehiclesService.insertBasicVehicles(basicVehicles));
    }

    /**
     * 修改汽车管理
     */
    @PreAuthorize("@ss.hasPermi('basic:vehicles:edit')")
    @Log(title = "汽车管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasicVehicles basicVehicles)
    {
        return toAjax(basicVehiclesService.updateBasicVehicles(basicVehicles));
    }

    /**
     * 删除汽车管理
     */
    @PreAuthorize("@ss.hasPermi('basic:vehicles:remove')")
    @Log(title = "汽车管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vehicleIds}")
    public AjaxResult remove(@PathVariable Long[] vehicleIds)
    {
        return toAjax(basicVehiclesService.deleteBasicVehiclesByVehicleIds(vehicleIds));
    }
}
